/**************************************************************************(C)*
 *
 *  This file is part of the GreenHills project
 *  Copyright (C) 2009  Tieto Corporation
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  Not for commmercial use. Only with Tieto permission.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *************************************************************************(C)*/

#pragma once

#include "iodll.h"

namespace greenhills {
namespace io {

    /**
     * @brief This class represents a message that has data and length.
     *
     * @author Alexander Nikolskiy (alexander.nikolskiy@tieto.com)
     */
    class GH_IO_CLASS CMessage
    {
    public:
        /**
         * @brief Initializes the instance.
         *
         * @param pData Pointer to a data to assign to the message.
         * @param pLength Length of the data.
         */
        CMessage(const char *pData,
                 unsigned int pLength,
                 bool pAddLengthToData = false);

        /**
         * @brief Destroys the instance.
         */
        virtual ~CMessage();

    public:
        /**
         * @brief Returns pointer to the data of message.
         *
         * @return Pointer to the data of message.
         */
        const char* getData();

        /**
         * @brief Returns pointer to the remaining data.
         *
         * Returns pointer to the remaining data. This method should
         * be used when processing data and in combination with
         * getPosition(), increasePosition() and getRemainingLength() methods.
         *
         * @return Pointer to the remaining unprocessed data.
         */
        const char* getRemainingData();

        /**
         * @brief Returns length of the whole data.
         *
         * @return Length of the whole data.
         */
        unsigned int getLength();

        /**
         * @brief Returns length of remaining data.
         *
         * Returns length of remaining data. This method should
         * be used when processing data and in combination with
         * getPosition(), increasePosition() and getRemainingData() methods.
         *
         * @return Length of the remaining unprocessed data.
         */
        unsigned int getRemainingLength();

        /**
         * @brief Returns position of pointer to the remaining
         * unprocessed data.
         *
         * Returns position of pointer to the remaining
         * unprocessed data. This method should be used when processing data
         * and in combination with increasePosition(), getRemainingData() and
         * getRemainingLength() methods.
         *
         * @return Position of the pointer to the remaining unprocessed data.
         */
        unsigned int getPosition();

        /**
         * @brief Increases position of pointer to the remaining
         * unprocessed data.
         *
         * Increases position of pointer to the remaining
         * unprocessed data. This method should be used when processing data
         * and in combination with getPosition(), getRemainingData() and
         * getRemainingLength() methods.
         *
         * @param pStep Amount of bytes of how much to increase the position.
         */
        void increasePosition(unsigned int pStep);

        /**
         * @brief Assigns data to message and sets length and position.
         */
        void setData(const char *pData,
                     unsigned int pLength,
                     bool pAddLengthToData = false);

        /**
         * @brief Deletes data of the message and sets length and position to 0.
         */
        void clear();

    private:
        /**
         * @brief Pointer to the data assigned to this message.
         */
        char *mData;

        /**
         * @brief Length of data assigned to this message.
         */
        unsigned int mLength;

        /**
         * @brief Position of pointer to the remaining unprocessed data.
         */
        unsigned int mPosition;
    };

} //namespace io
} //namespace greenhills
